//
//  DM_SQLSelectBuilder.swift
//  构建者模式_ORM_SWIFT
//
//  Created by macOS on 2017/4/22.
//  Copyright © 2017年 macOS. All rights reserved.
//

import Foundation




//例子
//select * from Student where name = "wsj2012"


//条件or  and
class DM_SQLSelectBuilder: DM_SQLBuilder {
    
    
    
    //查询所有
    func selectAllFromTableWhere( table: String, wh: [String : Any], orderBy: String? = nil, isAsc:Bool = true) -> DM_SQLSelectBuilder {
        let builder  = select().all().from().talbe(table).whAndFormat(wh) as! DM_SQLSelectBuilder
        if let tem = orderBy {
            builder.orderByWh(tem, isASC: isAsc)
        }
        
        return builder
    }
    
    
    
    //排序
    func orderByWh(_ colum: String, isASC: Bool = true) -> DM_SQLSelectBuilder {
        
        
        sql.append("order by \(colum) ")
        if isASC {
            sql.append("ASC ")
        }else{
            sql.append("DESC ")
        }
        
        return self
    }
    
   
}



extension DM_SQLSelectBuilder {
    func select() -> DM_SQLSelectBuilder {
        
        sql.append("select ")
        return self
    }
    
    
    func all() -> DM_SQLSelectBuilder {
        sql.append("* ")
        return self
    }
    
    func from() -> DM_SQLSelectBuilder {
        sql.append("from ")
        return self
    }
    
    
    fileprivate func orderBy() -> DM_SQLSelectBuilder {
        
        sql.append("order by ")
        
        return self
    }
    
   

}

    
    
    
    
    
    
    
    
    
    
    
    
    


